function hSpace(n, {
	spaceCode,
	spaceName,
	id
}, child) {
	// 检查是否已存在相同的键
	if (child && child[n]) {
		console.warn(`警告: 空间编码 ${n} 已存在，可能发生覆盖`);
	}

	// 添加错误处理
	try {
		if(child==null||child==undefined){
			return ;
		}
		// 添加楼层数量信息
		const floorCount = Object.keys(child).length;
		return {
			[n + '']: {
				id: id,
				spaceCode: spaceCode,
				spaceName: spaceName,
				floorCount, // 添加楼层数量
				...(child || {})  // 确保 child 不为 undefined
			}
		}
	} catch (error) {
		console.error(`创建空间编码失败: ${n}`, error);
		return {};
	}
}

// 使用 Set 或 Map 来检查重复
const usedCodes = new Set();

function checkDuplicate(code) {
	if (usedCodes.has(code)) {
		console.warn(`警告: 空间编码 ${code} 重复使用`);
		return true;
	}
	usedCodes.add(code);
	return false;
}

var bcode = {
	...hSpace("home", {
		spaceCode: "space-1010000000",
		spaceName: "先导工厂",
		id: 2581
	}, {
		...hSpace("MeiWei", {
			spaceCode: "space-1010500000",
			spaceName: "门卫",
			id: 2867
		}, {
			...hSpace("1", {
				id: 2831,
				spaceName: '门卫1层',
				spaceCode: 'space-1010601000'
			})
		}),
		...hSpace("A#_ChangFang", {
			spaceCode: "space-1010600000",
			spaceName: "先导工厂A#楼",
			id: 2868
		}, {
			...hSpace("3", {
				id: 2829,
				spaceName: 'A#厂房3层',
				spaceCode: 'space-1010603000'
			}),
			...hSpace("2", {
				id: 2830,
				spaceName: 'A#厂房2层',
				spaceCode: 'space-1010602000'
			}),
			...hSpace("1", {
				id: 2831,
				spaceName: 'A#厂房1层',
				spaceCode: 'space-1010601000'
			})
		}),
		...hSpace("B#_ChangFang", {
			spaceCode: "space-1010700000",
			spaceName: "先导工厂B#楼",
			id: 2869
		}, {
			...hSpace("3", {
				id: 2832,
				spaceName: 'B#厂房3层',
				spaceCode: 'space-1010703000'
			}),
			...hSpace("2", {
				id: 2833,
				spaceName: 'B#厂房2层',
				spaceCode: 'space-1010702000'
			}),
			...hSpace("1", {
				id: 2834,
				spaceName: 'B#厂房1层',
				spaceCode: 'space-1010701000'
			})
		}),
		...hSpace("C#_ChangFang", {
			spaceCode: "space-1010800000",
			spaceName: "先导工厂C#楼",
			id: 2870
		}, {
			...hSpace("3", {
				id: 2835,
				spaceName: 'C#厂房3层',
				spaceCode: 'space-1010803000'
			}),
			...hSpace("2", {
				id: 2836,
				spaceName: 'C#厂房2层',
				spaceCode: 'space-1010802000'
			}),
			...hSpace("1", {
				id: 2837,
				spaceName: 'C#厂房1层',
				spaceCode: 'space-1010801000'
			})
		}),
		...hSpace("D#_BanGongLou", {
			spaceCode: "space-1010900000",
			spaceName: "先导工厂D#办公楼",
			id: 2871
		}, {
			...hSpace("3", {
				id: 2838,
				spaceName: 'D#办公楼3层',
				spaceCode: 'space-1010903000'
			}),
			...hSpace("2", {
				id: 2839,
				spaceName: 'D#办公楼2层',
				spaceCode: 'space-1010902000'
			}),
			...hSpace("1", {
				id: 2840,
				spaceName: 'D#办公楼1层',
				spaceCode: 'space-1010901000'
			})
		})
	})
};

export function hGetCode(par, name) {
	try {
		let isB = name == null;
		let f;

		// 处理建筑物键名
		const buildingKey = par ? par.trim().replace(' ', '') : null;

		if (isB) {
			f = buildingKey ? bcode.home[buildingKey] : bcode.home;
		} else {
			if (!bcode.home[buildingKey]) {
				console.error(`建筑 ${par} 不存在，可用的建筑：`, Object.keys(bcode.home));
				return null;
			}

			const floorKey = (name * 1) + '';
			f = bcode.home[buildingKey][floorKey];
		}

		if (!f) {
			console.warn(`未找到对应的空间信息: ${par} ${name}`);
			return null;
		}

		// 返回时包含楼层数量
		return {
			id: f.id,
			spaceName: f.spaceName,
			spaceCode: f.spaceCode,
			isBuild: isB,
			buildName: isB ? f.spaceName : bcode.home[buildingKey].spaceName,
			floorCount: f.floorCount // 添加楼层数量
		};
	} catch (error) {
		console.error('获取空间编码失败:', error);
		return null;
	}
}